package com.example.peibei.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.Dynamic;
import com.dingtao.common.bean.RefreshResult;
import com.dingtao.common.bean.Square;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.FollowSquareDetailPresenter;
import com.example.peibei.service.presenter.FollowSquareListPresenter;
import com.example.peibei.service.presenter.SquareListPresenter;
import com.example.peibei.ui.activity.LoginActivity;
import com.example.peibei.ui.activity.SquareDetailActivity;
import com.example.peibei.ui.adapter.SquareListAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SquareListFragment extends WDFragment {
    @BindView(R.id.rv_get_order)
    EmptyRecyclerView rv_get_order;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;

    private String type;
    private List<Square.RecordsDTO> squareList = new ArrayList<>();
    private SquareListAdapter squareListAdapter;
    private SquareListPresenter squareListPresenter;
    private SPUtils spUtils;
    private String token;
    private FollowSquareListPresenter giveUpPresenter;

    public SquareListFragment(String type) {
        this.type = type;
    }

    @Override
    public String getPageName() {
        return "订单fragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_get_order;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        rv_get_order.setLayoutManager(new LinearLayoutManager(getActivity()));
        squareListAdapter = new SquareListAdapter(squareList);
        rv_get_order.setAdapter(squareListAdapter);
        rv_get_order.setEmptyView(empty_view);

        squareListPresenter = new SquareListPresenter(new SquareListCall());
        giveUpPresenter = new FollowSquareListPresenter(new FollowCall());

        squareListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                List<Square.RecordsDTO> data = (List<Square.RecordsDTO>) adapter.getData();
                Square.RecordsDTO recordsDTO = data.get(position);
                Bundle bundle = new Bundle();
                bundle.putString("imgUrl", recordsDTO.getHeaderUrl());
                bundle.putString("backUrl", recordsDTO.getBackUrl());
                bundle.putString("circleName", recordsDTO.getCircleName());
                bundle.putString("circleTypeId", recordsDTO.getCircleTypeId());
                bundle.putString("id", recordsDTO.getId() + "");
                bundle.putString("personNum", recordsDTO.getPersonNum());
                bundle.putBoolean("follow", recordsDTO.isFollow());
                intent(SquareDetailActivity.class, bundle);
            }
        });
        squareListAdapter.addChildClickViewIds(R.id.rl_follows);
        squareListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.rl_follows) {
                    if (!TextUtils.isEmpty(token)) {
                        List<Square.RecordsDTO> data = (List<Square.RecordsDTO>) adapter.getData();
                        Square.RecordsDTO recordsDTO = data.get(position);
                        boolean follow = recordsDTO.isFollow();
                        giveUpPresenter.reqeust(follow, "Bearer " + token, recordsDTO.getId()+"");
                    } else {
                        intent(LoginActivity.class);
                    }
                }
            }
        });
    }

    class FollowCall implements DataCall<Dynamic> {

        @Override
        public void success(Dynamic data, Object... args) {
            if (TextUtils.isEmpty(token)) {
                squareListPresenter.reqeust(true, "", type);
            } else {
                squareListPresenter.reqeust(true, "Bearer " + token, type);
            }
            QuanZiFragment parentFragment = (QuanZiFragment) getParentFragment();
            parentFragment.resumeFollowList();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            EventBus.getDefault().post(RefreshResult.getInstance(true, true));
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        token = spUtils.getString(SpConstant.USER_TOKEN);
        if (TextUtils.isEmpty(token)) {
            squareListPresenter.reqeust(true, "", type);
        } else {
            squareListPresenter.reqeust(true, "Bearer " + token, type);
        }
    }

    class SquareListCall implements DataCall<Square> {
        @Override
        public void success(Square data, Object... args) {
            //添加列表并刷新
            if (squareListPresenter.getPage() == 1) {
                squareList.clear();
            }
            squareListAdapter.setNewInstance(data.getRecords());
            squareListAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }
}
